<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">
      <!-- 添加商品 -->
      <el-row :gutter="20">
        <!-- span属性指的是占据多少列 -->
        <el-col :span="8">
          <!-- 搜索区域 -->
          <el-input
            v-model="queryInfo.query"
            placeholder="请输入内容"
            class="input-with-select"
            clearable
            @clear="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="warning" plain @click="toAddGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表表格 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="680"></el-table-column>
        <el-table-column align="center" prop="goods_price" label="商品价格(元)" width="120"></el-table-column>
        <el-table-column align="center" prop="goods_weight" label="商品重量" width="100"></el-table-column>
        <el-table-column align="center" prop="add_time" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" icon="el-icon-edit" type="warning" plain></el-button>
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              plain
              @click="removeGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'GoodsList',
  data() {
    return {
      queryInfo: {
        query: '', // 查询参数
        pagenum: 1, // 当前页码
        pagesize: 10 // 每页显示条数
      },
      goodsList: [], // 商品列表数据集合
      total: 0 // 所有的数据条数
    }
  },
  created() {
    // 调用获取商品列表数据的方法
    this.getGoodsList()
  },
  methods: {
    // 编程式导航实现跳转
    toAddGoods () {
      this.$router.push('/goods/addgood')
    },

    // 删除商品
    async removeGoods(id) {
      const confirmResult = await this.$confirm('是否将该商品放入回收管理, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirmResult !== 'confirm') return this.$message.info('您取消了删除')

      const { data: res } = await this.$http.delete(`/goods/${id}`)

      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.getGoodsList()
      this.$message.success(res.meta.msg)
    },

    // 监听当前显示的条数变更
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },

    // 监听当前页码的变更
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },

    // 获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('/goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)

      res.data.goods.forEach(item => {
        item.add_time = this.$dayjs(item.add_time).format('YYYY-MM-DD hh:mm')
      })

      this.goodsList = res.data.goods
      this.total = res.data.total
    }
  }
}
</script>

<style lang="less" scoped>
</style>
